
layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;
    var tableIns= table.render({
            elem: '#test'
            ,url:'/api/link/index'
            ,toolbar: '#toolbarDemo'
            ,title: '友情链接'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
                ,{field:'name', title:'博客名', width:150, edit: 'text'}
                ,{field:'logo', title:'图标', width:150, edit: 'text'}
                ,{field:'href', title:'链接', width:150, edit: 'text'}
                ,{field:'status', title:'状态', width:150, templet:function(d){
                    return '<input type="checkbox" name="is_top" lay-filter="is_top" lay-skin="switch" lay-text="是|否" checked="'+d.is_top+'">'
                }}
                ,{field:'created_at', title:'收录时间'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
            ]]
            ,page: true
        });
    //搜索
    $(".search_btn").on("click",function(){
        serialize=$('.layui-form').serializeArray();
        if($(".searchVal").val() != ''){
            table.reload("newsListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    key: serialize  //搜索的关键字
                }
            })
        }else{
            winui.window.msg("请输入搜索的内容");
        }
    });
    //是否置顶
    form.on('switch(is_top)', function(data){
        var index = layer.msg('修改中，请稍候',{icon: 16,time:false,shade:0.8});
        setTimeout(function(){
            if(data.elem.checked){
                winui.window.msg('启动置顶成功');
            }else{
                winui.window.msg('取消置顶成功');
            }
        },500);
    })
    //批量删除
    $(".delAll_btn").click(function(e){
        var self=this;
        winui.window.confirm('真的删除么', function(index){
            layer.close(index);
            var checkStatus = table.checkStatus("test");
            var data = checkStatus.data;
            var ids=[];
            $.each(data,function(i,item){
                ids[i]=item.id;
            })
            if(ids.length>0){
                data={
                    'ids':ids
                }
                var index='';
                $.ajax({
                    type:"post",
                    url:"/admin/link/del",
                    data:data,
                    beforeSend:function(){
                        index=layer.load(1, {
                            shade: [0.1,'#fff'] //0.1透明度的白色背景
                        });
                        $(self).attr('disabled',"disabled");
                    },
                    success: function(data){
                        if(data.code==200){
                            winui.window.msg('删除成功')
                            setTimeout(function(){
                                tableIns.reload();
                            },250)
                        }else{
                            winui.window.msg('删除失败');
                        }
                    },
                    complete:function(){
                        $(self).removeAttr("disabled");
                        layer.close(index);
                    }
                })
            }
        });


    });
    //添加
    $(".addNews_btn").click(function(e){
        addNews();
    })
    $(".reload_btn").click(function(){
        tableIns.reload();
    })
    //添加文章
    function addNews(edit=null){
        self=this;
        console.log(edit);
        if(!edit){
            var edit={
                'id':''
            };
        }
        winui.window.open({
            id: '公告',
            type: 2,
            title: '添加分类',
            area: ['70vw', '80vh'],
            content: "/admin/link/add?id="+edit.id,
            success:function(layero, index){
                var obj=layero.find("iframe")[0].contentWindow;
                if(edit.id){
                    obj.$('#id').val(edit.id);
                    obj.$('#logo').val(edit.logo);
                    obj.$(".linkLogoImg").attr('src',edit.logo);
                    obj.$('.linkName').val(edit.name);
                    obj.$('.linkUrl').val(edit.href);
                   var is_true=(edit.status==1) ? true:false;
                   console.log(is_true);
                    obj.$('#status').attr('checked',is_true);
                }
                obj.$('.layui-btn').click(function(){
                    setTimeout(function(){
                        layer.close(index);
                        layero.find('.layui-layer-ico.layui-layer-close').trigger('click');
                        tableIns.reload();
                    },1500)
                })
            }
        });
    }
    //头工具栏事件
    table.on('toolbar(test)', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        switch(obj.event){
            case 'getCheckData':
                var data = checkStatus.data;
                winui.window.alert(JSON.stringify(data));
                break;
            case 'getCheckLength':
                var data = checkStatus.data;
                winui.window.msg('选中了：'+ data.length + ' 个');
                break;
            case 'isAll':
                winui.window.msg(checkStatus.isAll ? '全选': '未全选');
                break;
        };
    });

    //监听行工具事件
    table.on('tool(test)', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){
            data = {'ids': data.id};
            winui.window.confirm('确定删除此？', {icon: 3, title: '提示信息'}, function (index) {
                winui.window.post("/admin/link/del", data, function (data) {
                    winui.window.msg(data.msg);
                    tableIns.reload();
                })
            })
        } else if(obj.event === 'edit'){
            addNews(data);
        }
    });

});
